package com.nova.android.tvlauncher.ui.view.atv

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.nova.android.tvlauncher.databinding.ViewCustomHomeBinding
import com.nova.android.tvlauncher.R

class CustomHomeAtvView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

     val binding: ViewCustomHomeBinding =
        ViewCustomHomeBinding.inflate(LayoutInflater.from(context), this, true)

    init {
        initAttrs(context, attrs)
        binding.root.setOnClickListener {
            cusChannelClickListener?.invoke()
        }
        binding.root.onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
            binding.tvTitle.isSelected = hasFocus
            binding.tvSubtitle.isSelected = hasFocus
            binding.tvAction.isSelected = hasFocus
        }

    }

    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        context.obtainStyledAttributes(attrs, R.styleable.CustomHomeAtvView).apply {
            binding.tvTitle.text = getString(R.styleable.CustomHomeAtvView_custom_home_title)
            binding.tvSubtitle.text = getString(R.styleable.CustomHomeAtvView_custom_home_subtitle)
            binding.tvAction.text = getString(R.styleable.CustomHomeAtvView_custom_home_action_text)
            recycle()
        }
    }

    fun setTitle(text: String) {
        binding.tvTitle.text = text
    }

    fun setSubtitle(text: String) {
        binding.tvSubtitle.text = text
    }

    fun setActionText(text: String) {
        binding.tvAction.text = text
    }

    private var cusChannelClickListener: (() -> Unit)? = null

    fun setCusChannelClickListener(listener: () -> Unit) {
        cusChannelClickListener = listener
    }
}
